// pages/aboutme/aboutme.js
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    binded: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setHeight();
    this.loadUserInfo();
    
  },
  /**
   * 加载用户信息
   */
  loadUserInfo() {
    // 1.处理头像以外的信息
    let binded = wx.getStorageSync('binded');
    if (binded) {
      this.setData({
        binded,
        avatar: wx.getStorageSync('avatar'),
        dept: wx.getStorageSync('dept'),
        name: wx.getStorageSync('name'),
        plateNumber: wx.getStorageSync('plateNumber'),
        phone: wx.getStorageSync('phone')
      })
    } else {
      this.setData({
        binded,
        avatar: wx.getStorageSync('avatar'),
        dept: '未知',
        name: '未知',
        plateNumber: '未知',
        phone: wx.getStorageSync('phone')
      })
    }

    // 2.处理头像信息
    let avatar = wx.getStorageSync('avatar');
    if (avatar) {
      this.setData({
        avatar, hasAvatar: true
      });
    } else {
      this.setData({
        hasAvatar: false
      })
    }
  },
  getUserInfo: function() {
    let self = this;
    wx.getUserProfile({
      lang: 'zh_CN',
      desc:'test',
      success(res) {
        wx.setStorageSync('avatar', res.userInfo.avatarUrl);
        self.setData({
          avatar: res.userInfo.avatarUrl,
          hasAvatar: true
        });
      },
      fail() {
        wx.showToast({
          title: '网络出错',
          icon: 'error',
          duration: 2000 
        })
      }
    })
  },
  /**
   * 设置高度
   */
  setHeight() {
    let {windowHeight} = wx.getSystemInfoSync();
    this.setData({
      // 使用自定义导航栏时页面的高度
      // windowHeight: (windowHeight - wx.getStorageSync('statusBarHeight')
      //     - wx.getStorageSync('navigationBarHeight')) + 'px',
      // 不使用自定义导航栏时页面的高度
      windowHeight: windowHeight + 'px'
    })
  },
  logout() {
    wx.clearStorageSync();
    wx.removeStorageSync('avatar');
    wx.navigateTo({
      url: '/pages/index/index',
    })
  }
})